import { Button, Toast } from "antd-mobile";
import { reqgetgoodsinfo, reqcartadd } from "../../http/api";

//state
const initState = {
  list: [],
  goodse: ["商品", "详情", "评价", "推荐"],
};

//action type
const TYPES = {
  DETAIL_CHANGE_LIST: "DETAIL_CHANGE_LIST",
};

//reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    case TYPES.DETAIL_CHANGE_LIST:
      return {
        ...state,
        list: action.list,
      };
    default:
      return state;
  }
};

//action creator
export const actions = {
  changeList: (list) => ({ type: TYPES.DETAIL_CHANGE_LIST, list }),
  reqList: (id) => (dispatch, getState) => {
    reqgetgoodsinfo({ id }).then((res) => {
      dispatch(actions.changeList(res.data.list[0]));
    });
  },
  addShop: (params) => (dispatch) => {
    reqcartadd({
      uid: JSON.parse(localStorage.getItem("isLogin")).uid,
      goodsid: params.id,
      num: 1,
      type: 2,
    }).then((res) => {
        Toast.show({
            content: res.data.msg,
          });
    });
  },
};

//selector导出数据给组件
export const getList = (state) => state.detail.list;
export const getGood = (state) => state.detail.goodse;

//导出reducer
export default reducer;
